package com.hdyg.csc.activity.common;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

import com.hdyg.csc.activity.WelcomeAty;
import com.hdyg.csc.mvp.base.BasePresenter;
import com.hdyg.csc.ui.CustomProgressDialog;
import com.hdyg.csc.util.SysStyleUtil;

import butterknife.ButterKnife;

/**
 * 基类  用于所有的activity继承
 *
 * @author 陈白衣
 * @time 2018/3/8 11:30
 */
public abstract class BaseActivity<V, T extends BasePresenter<V>> extends FragmentActivity {
    protected static final String TAG = "czb";
    protected static final String ERROR = "error";
    private ForceOffLineReceiver receive;
    public static final String UN_TOKEN_BROCAST = "com.hdyg.cby.OFF";
    protected Context mContext;

    protected T mPresenter; // Presenter 对象
    private CustomProgressDialog progressDialog;


    protected <T extends View> T findView(int id) {
        return (T) findViewById(id);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setNoTitile();//取消标题的方法
        ActivityConllector.addActivity(this);
        // 沉浸状态栏
        steepStatusBar();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//设置竖屏显示
        setContentView(getLayoutId());
        mContext = this;
        ButterKnife.bind(this);
        initView();
        initData();
        mPresenter = createPresenter();
        if (mPresenter != null) {
            mPresenter.attachView((V) this);
        }

        progressDialog = createProgress();

    }
    //设置沉浸式状态栏
    private void steepStatusBar() {
        //api >= 19
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //设置状态栏颜色
            SysStyleUtil.setStatusBarLightMode(this, android.R.color.transparent);
        }
    }
    protected abstract int getLayoutId();
    protected abstract void initView();
    protected abstract void initData();
    /**
     * 创建 Presenter 对象
     *
     * @return
     */
    protected abstract T createPresenter();

    protected abstract CustomProgressDialog createProgress();

    public void setNoTitile() {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    }

    //封装toast
    public void toastNotifyShort(String notify) {
        Toast.makeText(getApplicationContext(), notify, Toast.LENGTH_SHORT).show();
    }

    public void toastNotifyLong(String notify) {
        Toast.makeText(getApplicationContext(), notify, Toast.LENGTH_LONG).show();
    }

    public void startActivity(Class<?> pClass){
        Intent mIntent=new Intent(this,pClass);
        this.startActivity(mIntent);
    }
    /**
     *携带数据的页面跳转
     * @param clz
     * @param bundle
     */
    public void startActivity(Class<?> clz, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, clz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }
    /**
     * 含有Bundle通过Class打开编辑界面
     *
     * @param cls
     * @param bundle
     * @param requestCode
     */
    public void startActivityForResult(Class<?> cls, Bundle bundle,
                                       int requestCode) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(UN_TOKEN_BROCAST);
        receive = new ForceOffLineReceiver();
        registerReceiver(receive, intentFilter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (receive != null) {
            unregisterReceiver(receive);
            receive = null;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
        if (progressDialog != null) {
            progressDialog.cancel();
            progressDialog = null;
        }
        ActivityConllector.removeActivity(this);
    }

    class ForceOffLineReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(final Context context, final Intent intent) {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setTitle("警告");
            builder.setMessage("您的设备在其他设备登录, 请您重新登录!");
            builder.setCancelable(false); // 将对话框设置为不可取消
            // 给按钮添加注册监听
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // 点击按钮所调用的方法
                    ActivityConllector.finishAll();//销毁所有的活动
                    Intent intent = new Intent(context, WelcomeAty.class);
                    context.startActivity(intent);
                }
            });
            builder.show();
        }
    }
}
